package com.mi.rof.engineer.service.impl;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mi.rof.engineer.entity.EngineerTable;
import com.mi.rof.engineer.mapper.EngineerMapper;
import com.mi.rof.engineer.service.EngineerService;
import com.mi.rof.entity.*;
import com.mi.rof.entity.enums.ExceptionEnum;
import com.mi.rof.entity.enums.RoleEnum;
import com.mi.rof.entity.vo.UserInfoVo;
import com.mi.rof.entity.param.UserParam;
import com.mi.rof.utils.JwtUtils;
import com.mi.rof.utils.UserThreadLocal;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class EngineerServiceImpl implements EngineerService {

    @Resource
    private EngineerMapper engineerMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result<String> login(UserParam userParam) {
        String userId = userParam.getUserId();
        if (userId != null) {
            LambdaQueryWrapper<EngineerTable> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(EngineerTable::getEngineerId, userId);
            EngineerTable engineerTable = engineerMapper.selectOne(queryWrapper);
            String userPassword = userParam.getPassword();
            //对password进行md5加密
            String encodedPassword = MD5.create().digestHex(userPassword);
            if (engineerTable.getPassword().equals(encodedPassword)) {
                //校验通过
                UserInfoVo userInfoVo = new UserInfoVo();
                BeanUtils.copyProperties(engineerTable, userInfoVo);
                userInfoVo.setUserId(engineerTable.getEngineerId());
                userInfoVo.setUsername(engineerTable.getEngineerName());
                userInfoVo.setUserRole(engineerTable.getRole());
                userInfoVo.setUserRole(RoleEnum.getByMsg(userParam.getRole()).getCode());
                String token = JwtUtils.createToken(userInfoVo);
                userInfoVo.setToken(token);
                //放入redis缓存标识状态
                stringRedisTemplate.opsForValue().set(userId, token);
                //设置过期时间
                stringRedisTemplate.expire(userId,  System.currentTimeMillis()+1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS);
                //放入threadLocal中
                UserThreadLocal.put(userInfoVo);
                return Result.successResult(token);
            }else{
                throw new SystemException(ExceptionEnum.PASSWORD_ERROR.getCode(), ExceptionEnum.PASSWORD_ERROR.getMsg());
            }
        }
        //不存在抛出异常
        throw new SystemException(ExceptionEnum.USER_NOT_EXIST.getCode(), ExceptionEnum.USER_NOT_EXIST.getMsg());
    }

    @Override
    public Result checkEngineerAuth(UserInfoVo userInfoVo) {
        if(RoleEnum.ENGINEER.getCode()==userInfoVo.getUserRole()){
            return Result.successResult();
        }else {
            return Result.fail(ExceptionEnum.NO_AUTH.getCode(), ExceptionEnum.NO_AUTH.getMsg());
        }

    }
}
